﻿using System;
using System.Windows.Threading;
using Caliburn.Micro;
using FinalCountdownServices.Models;

namespace FinalCountdownServices.ViewModels
{
    public class EventPageViewModel : Screen
    {
        public int EventId { get; set; }

        private EventModel myEvent;
        public EventModel MyEvent
        {
            get { return myEvent; }
            set
            {
                myEvent = value;

            }
        }


        public string Title
        {
            get { return MyEvent == null ? "" : myEvent.Title; }

        }
        public string Description
        {
            get { return MyEvent == null ? "" : MyEvent.Description; }
        }
        public bool ShowTimer { get { return MyEvent != null && !MyEvent.Obsolete; } }
        public bool Obsolete { get { return MyEvent != null && MyEvent.Obsolete; } }

        //public bool TileEnabled
        //{
        //    get
        //    {
        //        var tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("EventId=" + EventId));
        //        return tile != null;
        //    }
        //    set { return; }
        //}
        //public string TileEnabledStr
        //{
        //    get { return TileEnabled.ToString(); }
        //    set { return; }
        //}


        public int RemainingDays { get { return MyEvent == null ? 0 : MyEvent.RemainingDays; } }
        public int RemainingHours { get { return MyEvent == null ? 0 : MyEvent.RemainingHours; } }
        public int RemainingMinutes { get { return MyEvent == null ? 0 : MyEvent.RemainingMinutes; } }
        public int RemainingSeconds { get { return MyEvent == null ? 0 : MyEvent.RemainingSeconds; } }


        private IEventService _eventService;
        private DispatcherTimer myTimer;


        public EventPageViewModel(IEventService eventService)
        {
            _eventService = eventService;
            myTimer = new DispatcherTimer();
        }

        protected override void OnActivate()
        {
            Init();
        }

        public void Init()
        {
            myEvent = _eventService.GetEvent(EventId);
            NotifyOfPropertyChange(() => Title);
            NotifyOfPropertyChange(() => Description);
            NotifyOfPropertyChange(() => ShowTimer);
            NotifyOfPropertyChange(() => Obsolete);
            StartTimer();
        }

        private void StartTimer()
        {

            myTimer.Interval = TimeSpan.FromSeconds(1);
            myTimer.Tick += (s, e) => Notify();
            myTimer.Start();


        }

        protected override void OnDeactivate(bool close)
        {
            myTimer.Stop();
        }


        private void Notify()
        {
            NotifyOfPropertyChange(() => RemainingDays);
            NotifyOfPropertyChange(() => RemainingHours);
            NotifyOfPropertyChange(() => RemainingMinutes);
            NotifyOfPropertyChange(() => RemainingSeconds);
        }



    }
}
